Skip to content

fix(useTemplateRef): handle useTemplateRef edge case with vFor #12733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Jan 17, 2025

close #12731

the underlying problem is

const elements1 = useTemplateRef('testref') 
// instance.refs['testref'] is a shallowRef
instance.refs['testref'].value = [1]  // value is an array

const elements2 = ref() 
// instance.refs['elements2'] is a ref
instance.refs['elements2'].value = [1]  // value is a reactive array

Copy link

github-actions bot commented Jan 17, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 100 kB (+4 B) 38 kB (+7 B) 34.2 kB (-7 B)
vue.global.prod.js 158 kB (+4 B) 57.8 kB (+3 B) 51.4 kB (-5 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.4 kB (+32 B) 18.2 kB (+5 B) 16.6 kB (-16 B)
createApp 54.4 kB (+32 B) 21.2 kB (+12 B) 19.3 kB (+13 B)
createSSRApp 58.6 kB (+32 B) 22.9 kB (+4 B) 20.9 kB (+9 B)
defineCustomElement 59.2 kB (+32 B) 22.8 kB (+8 B) 20.7 kB (+8 B)
overall 68.4 kB (+32 B) 26.3 kB (+9 B) 24 kB (-25 B)

Copy link

pkg-pr-new bot commented Jan 17, 2025

Open in Stackblitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@12733

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@12733

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@12733

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@12733

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@12733

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@12733

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@12733

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@12733

@vue/shared

npm i https://pkg.pr.new/@vue/shared@12733

vue

npm i https://pkg.pr.new/vue@12733

@vue/compat

npm i https://pkg.pr.new/@vue/compat@12733

commit: 2336e2a

@edison1105 edison1105 added ready to merge The PR is ready to be merged. 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Jan 17, 2025
@skirtles-code
Copy link
Contributor

I'm not sure whether it would be considered a problem, but this change would also impact $refs.

@edison1105
Copy link
Member Author

I'm not sure whether it would be considered a problem, but this change would also impact $refs.

Thanks for your review. Perhaps this cannot be regarded as a problem, because this kind of behavior already exists in PROD.

@skirtles-code
Copy link
Contributor

I would be nervous about drawing any conclusions based on that example. getCurrentInstance() is undocumented, so to hit that edge case using documented APIs you'd need to mix <script setup> and the Options API. Not only is it inconsistent between DEV and PROD, it also behaves differently depending on whether you use ref() or ref([]):

It strikes me as an unintentional edge case, rather than something we should be using as precedent.


I'm not entirely clear why $refs isn't reactive, but personally I think it's a decision that should be revisited. That might also allow the implementation of useTemplateRef to be simplified too.

But, given $refs currently isn't reactive, I'd be hesitant to introduce partial reactivity in just the v-for case unless we're planning to move towards full reactivity in future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. ready to merge The PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useTemplateRef returns ShallowRef instead of Ref
3 participants